package com.mars.admin.modules.test;

import com.mars.admin.framework.common.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * CORS测试控制器
 * 用于测试跨域配置是否正常工作
 *
 * @author Mars
 * @version 1.0
 * @date 2025-01-07
 */
@Slf4j
@RestController
@RequestMapping("/test/cors")
@Tag(name = "CORS测试", description = "用于测试跨域配置的接口")
public class CorsTestController {

    @GetMapping("/simple")
    @Operation(summary = "简单GET请求测试", description = "测试简单的GET请求是否能正常跨域")
    public Result<Map<String, Object>> simpleGet() {
        Map<String, Object> data = new HashMap<>();
        data.put("message", "CORS GET请求测试成功");
        data.put("timestamp", LocalDateTime.now());
        data.put("method", "GET");
        
        log.info("处理CORS GET测试请求");
        return Result.success(data);
    }

    @PostMapping("/complex")
    @Operation(summary = "复杂POST请求测试", description = "测试带有自定义头的POST请求是否能正常跨域")
    public Result<Map<String, Object>> complexPost(@RequestBody(required = false) Map<String, Object> requestData) {
        Map<String, Object> data = new HashMap<>();
        data.put("message", "CORS POST请求测试成功");
        data.put("timestamp", LocalDateTime.now());
        data.put("method", "POST");
        data.put("receivedData", requestData);
        
        log.info("处理CORS POST测试请求，接收数据: {}", requestData);
        return Result.success(data);
    }

    @PutMapping("/put-test")
    @Operation(summary = "PUT请求测试", description = "测试PUT请求是否能正常跨域")
    public Result<Map<String, Object>> putTest(@RequestBody(required = false) Map<String, Object> requestData) {
        Map<String, Object> data = new HashMap<>();
        data.put("message", "CORS PUT请求测试成功");
        data.put("timestamp", LocalDateTime.now());
        data.put("method", "PUT");
        data.put("receivedData", requestData);
        
        log.info("处理CORS PUT测试请求，接收数据: {}", requestData);
        return Result.success(data);
    }

    @DeleteMapping("/delete-test")
    @Operation(summary = "DELETE请求测试", description = "测试DELETE请求是否能正常跨域")
    public Result<Map<String, Object>> deleteTest() {
        Map<String, Object> data = new HashMap<>();
        data.put("message", "CORS DELETE请求测试成功");
        data.put("timestamp", LocalDateTime.now());
        data.put("method", "DELETE");
        
        log.info("处理CORS DELETE测试请求");
        return Result.success(data);
    }

    @RequestMapping(value = "/options-test", method = RequestMethod.OPTIONS)
    @Operation(summary = "OPTIONS请求测试", description = "测试OPTIONS预检请求是否能正常处理")
    public Result<Map<String, Object>> optionsTest() {
        Map<String, Object> data = new HashMap<>();
        data.put("message", "CORS OPTIONS请求测试成功");
        data.put("timestamp", LocalDateTime.now());
        data.put("method", "OPTIONS");
        
        log.info("处理CORS OPTIONS测试请求");
        return Result.success(data);
    }
} 